package la.bbs558.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import la.bbs558.dao.UserRoleDao;
import la.bbs558.entity.User;
import la.bbs558.entity.UserRole;
import la.bbs558.exception.MsgException;
import la.bbs558.service.UserRoleService;
import la.bbs558.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户表(UserRole)表服务实现类
 *
 * @author weihanmin
 * @since 2023-12-14 17:58:16
 */
@Service("userRoleService")
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRole> implements UserRoleService {

    @Resource
    private UserService userService;

    @Override
    public UserRole initUserRole(Long userId, String role) {
        Integer count = userService.lambdaQuery().eq(User::getId, userId).count();
        if (count == 0) {
            throw new MsgException("用户不存在");
        }
        UserRole userRole = new UserRole()
                .setUserId(userId)
                .setRoleIds(role);
        userRole.insert();
        return getById(userRole.getId());
    }
}

